<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'applysend.add'" ></el-button>
                <el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length == 0" @click="batch_del" v-auth="'applysend.delete'"></el-button>
                <sc-export-button :auth="'applysend.export'" :apiObj="apiObj" :search="search" :selection="selection"></sc-export-button>
                <div class="finish">
                    <div class="finishNum">总加工费:
                        <div class="text">{{ total_stock.total_process_price }}</div>
                    </div>
                </div>
            </div>
            <div class="right-panel">
                <div class="right-panel-search">
                    <!-- <el-input v-model="search.send_code" placeholder="发货单号" clearable @keyup.enter="upsearch"></el-input> -->
                    <el-input v-model="search.search_keyword"  class="input-with-search" style="width: 300px" @keyup.enter="upsearch">
                        <template #prepend>
                            <el-select v-model="search.search_field" filterable clearable placeholder="请选择" style="width: 110px">
                                <el-option label="发货单号" value="send_code"/>
                                <el-option label="条码" value="roll_code"/>
                                <el-option label="车牌号" value="car_code"/>
                                <el-option label="司机电话" value="car_mobile"/>
                            </el-select>
                        </template>
                    </el-input>
                    <el-select v-model="search.ware_id" placeholder="请选择" filterable clearable>
                        <el-option v-for="item in wareList" :key="item.id" :label="item.ware_name" :value="item.id"></el-option>
                    </el-select>
                    <el-select v-model="search.status" placeholder="状态" clearable>
                        <el-option label="待审" :value="0"></el-option>
                        <el-option label="通过" :value="1"></el-option>
                        <el-option label="全部" :value="-1"></el-option>
                    </el-select>
                    <el-date-picker v-model="search.send_date" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" />
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" :apiObj="apiObj" row-key="id" @selection-change="selectionChange" @setExtend="setExtend" tableName="applySendTable" :column="sendColumn">
                <el-table-column type="selection" width="50" align="center"></el-table-column>
                <!-- <el-table-column label="序号" type="index" width="60" align="center"></el-table-column> -->
                <template #send_code="{row}">
                    <div class="display">
                        <div style="width: 60%">
                            <div style="display: flex;align-items: center;">单号：
                                <el-link :underline="false" type="primary" @click="table_show(row)">{{ row.send_code }}</el-link>
                                <el-icon class="icon" :size="20" @click="common.copy(row.send_code)"><el-icon-Edit /></el-icon>
                                <!-- <el-icon class="icon" color="#1e9fff" :size="18" @click="send_bell(row)" v-auth="'applysend.message'"><el-icon-BellFilled /></el-icon> -->
                            </div>
                        </div>
                        <div style="width: 40%">
                            <div class="margin">总匹数：<span>{{row.total_count}}</span> </div>
                        </div>
                    </div>
                    <div class="display">
                        <div class="margin">车牌号：{{row.car_code}}
                        </div>
                        <div style="width: 40%">
                            <div class="margin">总码数：<span>{{row.total_num}}</span> </div>
                        </div>
                    </div>
                    <div class="display">
                        <div class="margin">司机电话：<span>{{row.car_mobile}}</span> </div>
                        <div style="width: 40%">
                            <div class="margin">总加工费：<span>{{row.total_price}}</span> </div>
                        </div>
                    </div>
                    <div class="display">
                        <div style="display: flex;align-items: center;">备注：
                            <el-icon class="icon" color="#1e9fff" :size="18" v-if="row.status == 0 || row.status == 1" @click="table_write(row,'remark','备注')"><el-icon-EditPen /></el-icon>
                            <el-popover placement="top-start" :width="230" trigger="hover" :content="row.remark">
                                <template #reference>
                                    <div style="width: 170px;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">{{row.remark}}</div>
                                </template>
                            </el-popover>
                        </div>
                        <div style="width: 40%">
                            <div class="margin">业务员：<span>{{row.admin?.name}}</span> </div>
                        </div>
                    </div>
                </template>
                <template #status="{row}">
                    <div class="column">
                        <el-button :type="row.status == 0 ? 'warning' : row.status == 1 ? 'primary' : 'danger'" size="small" @click="send_check(row)" v-auth="'applysend.status'">{{row.status == 0 ? '待审' : row.status == 1 ? '通过' : '退回'}}</el-button>
                        <el-button type="primary" size="small" @click="send_upload(row)" v-if="row.status == 1" v-auth="'applysend.upload'">查看</el-button>
                    </div>
                </template>
                <template #operation="scope">
                    <div class="buttonFlex">
                        <div class="btn">
                            <div>
                                <el-button type="success" size="small" @click="table_edit(scope.row)" v-auth="'applysend.edit'" v-if="scope.row.status == 0">编辑</el-button>
                                <!-- <el-button type="primary" size="small" @click="send_check(scope.row)" v-auth="'applysend.status'">状态</el-button> -->
                                <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                                    <template #reference>
                                        <el-button type="danger" size="small" v-auth="'applysend.delete'" v-if="scope.row.status == 0">删除</el-button>
                                    </template>
                                </el-popconfirm>
                            </div>
                            <div>
                                <el-button size="small" @click="table_container(scope.row)" v-auth="'applysend.container'">汇总码单</el-button>
                                <el-button size="small" @click="table_code_detail(scope.row)" v-auth="'applysend.detail'">明细码单</el-button>
                            </div>
                        </div>
                    </div>
                </template>
            </scTable>
        </el-main>
    </el-container>
    <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>
    <check-status v-if="dialog.check" ref="checkDialog" @success="handleSuccess" @closed="dialog.check = false" draggable></check-status>
    <notice-save v-if="dialog.notice" ref="noticeDialog" @success="handleSuccess" @closed="dialog.notice = false"></notice-save>
    <edit-save v-if="dialog.edit" ref="editDialog" @success="handleSuccess" @closed="dialog.edit = false"></edit-save><!-- 备注等... -->
    <status-list v-if="dialog.list" ref="listDialog" @success="handleSuccess" @closed="dialog.list = false" :query="{id: id}"></status-list><!-- 状态修改记录 -->
    <send-print v-if="dialog.print" ref="printDialog" @success="handleSuccess" @closed="dialog.print = false"></send-print>
    <sendmater-print v-if="dialog.print2" ref="print2Dialog" @success="handleSuccess" @closed="dialog.print2 = false"></sendmater-print>
    <upload-dialog v-if="dialog.upload" ref="uploadDialog" @success="handleSuccess" @closed="dialog.upload = false"></upload-dialog>
</template>

<script>
import CheckStatus from './checkStatus.vue'
import saveDialog from './save'
import NoticeSave from '../../noticeSave.vue'
import EditSave from './editSave.vue'
import StatusList from './statusList.vue'
import SendPrint from './sendPrint.vue'
import SendmaterPrint from './sendmaterPrint.vue'
import uploadDialog from '../../upload.vue'

export default {
    name: '采购计划管理',
    components: {
        saveDialog, CheckStatus, NoticeSave, EditSave, StatusList, SendmaterPrint, SendPrint, uploadDialog,
    },
    data() {
        return {
            dialog: {
                save: false,
                check: false,
                look: false,
                upload: false,
                notice: false,
                edit: false,
                list: false,
                print: false,
                print2: false,
            },
            apiObj: this.$API.ware.ware.applysend.list,
            selection: [],
            search: {
                search_keyword: null,
                search_field: null, 
                send_code: null,
                send_date: [this.moment().startOf('year').format('YYYY-MM-DD'), this.moment().endOf('year').format('YYYY-MM-DD')],
                status: null,
                goods_name: null
            },
            id: "",
            today: new Date(),
            total_stock: {
                total_process_price: 0,
            },
            sendColumn: [
                { label: "订单信息", prop: "send_code", align: 'left',},
                // { label: "总码数", prop: "total_count",width: "130"},
                // { label: "总匹数", prop: "total_num",width: "130"},
                // { label: "总加工费", prop: "total_money",width: "130"},
                // { label: "业务员", prop: "admin.name",width: "130"},
                { label: "目标仓库", prop: "ware.ware_name",width: "130", },
                { label: "审核", prop: "status",width: "130", },
                { label: "操作", prop: "operation", width: "220",fixed: 'right'},
            ],
            wareList: [],
        }
    },
    mounted() {
        if(this.$route.query.code) {
            this.search.send_code = this.$route.query.code
            this.upsearch()
        }
		this.getWare();
    },
    methods: {
        setExtend(data){
            this.total_stock = data
        },
        //添加
        add() {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open()
            })
        },
        //编辑
        table_edit(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('edit').setData(row)
            })
        },
        //查看
        table_show(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('show').setData(row)
            })
        },
        //删除
        async table_del(row) {
            var reqData = { id: row.id }
            var res = await this.$API.ware.ware.applysend.delete.post(reqData);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("删除成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },
        async table_del_all(delete_ids) {
            var reqData = { id: delete_ids }
            var res = await this.$API.ware.ware.applysend.delete.post(reqData);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("删除成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },
        //批量删除
        async batch_del() {
            var delete_ids = [];
            this.selection.forEach((item) => {
                delete_ids.push(item.id)
            })

            this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
                type: 'warning'
            }).then(() => {
                this.table_del_all(delete_ids)
            }).catch(() => {

            })
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selection = selection;
        },
        //搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        refresh() {
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.search.send_date = [this.moment().startOf('year').format('YYYY-MM-DD'), this.moment().endOf('year').format('YYYY-MM-DD')],
            this.$refs.table.upData(this.search)
        },
        //本地更新数据
        handleSuccess(data, mode) {
            console.log(mode);
            if (mode == 'add') {
                this.$refs.table.refresh()
            } else if (mode == 'edit') {
                this.$refs.table.refresh()
            } else if (mode == 'applysend') {
                this.$refs.table.refresh()
            }
        },
        // 入库提醒
        send_bell(row){
            let data = {
                id: row.id,
                code: row.send_code,
                apply_id: row.apply_id
            }
            this.dialog.notice = true
            this.$nextTick(() => {
                this.$refs.noticeDialog.open('edit').setData(data,'发货单',7)
            })
        },
        // 备注,编号,箱号等
        table_write(row,text,font){
            this.dialog.edit = true
            this.$nextTick(() => {
                this.$refs.editDialog.open('edit').setData(row,text,font)
            })
        },
        // 状态
        send_check(row){
            this.dialog.check = true
            this.$nextTick(() => {
                this.$refs.checkDialog.open('applysend').setData(row)
            })
        },
        // 查看文件
        send_upload(row){
            this.dialog.upload = true
            this.$nextTick(() => {
                this.$refs.uploadDialog.open('show').setData(row)
            })
        },
        // 货柜码单
        table_container(row){
            this.dialog.print = true
            this.$nextTick(() => {
                this.$refs.printDialog.open('edit').setData(row)
            })
        },
        // 明细码单
        table_code_detail(row){
            this.dialog.print2 = true
            this.$nextTick(() => {
                this.$refs.print2Dialog.open('edit').setData(row)
            })
        },
        // 仓库列表
		async getWare() {
			var res = await this.$API.basic.ware.all.get();
			this.wareList = res.data
		},
    }
}
</script>

<style lang="scss">
.margin{
    margin: 5px 0;
}
.display {
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.btn{
    display: flex;
    div{
        display: flex;
        flex-direction: column;
        margin-left: 12px;
    }
    button{
        margin-left: 0 !important;
        margin-bottom: 5px;
        width: fit-content;
    }
}
.status{
    padding: 2px 5px;
    font-size: 14px;
    border: 1px solid #000;
    cursor: pointer;
}
.el-popper.is-customized {
    /* Set padding to ensure the height is 32px */
    padding: 6px 12px;
    background: #78ba32;
}

.el-popper.is-customized .el-popper__arrow::before {
    background: #78ba32;
    right: 0;
}
.tooltip{
    font-size: 14px;
    color: #fff;
}
</style>
